package com.data.utill;

import com.data.annotation.CsvExport;
import com.data.annotation.CsvHeader;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * Created by silenceholic on 2017/5/8.
 */
public class ReflectUtil {

    /**
     * 防止被构造
     */
    private ReflectUtil() {
    }

    private static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});

            if (value instanceof Date) {
                value = DateUtil.formate((Date) value, "MEDIUM");
            }

            return value;
        } catch (Exception e) {
//            log.error(e.getMessage(),e);
            return null;
        }
    }

    /**
     * 获取属性名数组
     */
    private static String[] getFieldName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        int index = 0;
        CsvExport csvExportObject = o.getClass().getAnnotation(CsvExport.class);

        if (csvExportObject == null) {
            for (int i = 0; i < fields.length; i++) {
                fieldNames[i] = fields[i].getName();
            }
        } else {
            for (int i = 0; i < fields.length; i++) {
                CsvExport csvExportField = fields[i].getAnnotation(CsvExport.class);
                if (null != csvExportField && csvExportField.export()) {
                    fieldNames[index] = fields[i].getName();
                    index++;
                }
            }
        }

        return fieldNames;
    }

    /**
     * 获取属性类型(type)，属性名(name)，属性值(value)的map组成的list
     */
    private static List getFieldsInfo(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        List list = new ArrayList();
        Map infoMap = null;
        for (int i = 0; i < fields.length; i++) {
            infoMap = new HashMap();
            infoMap.put("type", fields[i].getType().toString());
            infoMap.put("name", fields[i].getName());
            infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
            list.add(infoMap);
        }
        return list;
    }

    /**
     * 获取对象的所有属性值，返回一个对象数组
     */
    public static Object[] getFieldValues(Object o) {
        String[] fieldNames = getFieldName(o);
        Object[] value = new Object[fieldNames.length];
        for (int i = 0; i < fieldNames.length; i++) {
            value[i] = getFieldValueByName(fieldNames[i], o);
        }
        return value;
    }

    /**
     * 获取对象header，返回一个对象数组
     */
    public static Object[] getFieldHeaders(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] headers = new String[fields.length];
        int index = 0;
        CsvExport csvExportObject = o.getClass().getAnnotation(CsvExport.class);

        if (csvExportObject == null) {
            for (int i = 0; i < fields.length; i++) {
                CsvHeader csvExportHeader = fields[i].getAnnotation(CsvHeader.class);
                if (null != csvExportHeader) {
                    headers[i] =csvExportHeader.name();
                }
            }
        } else {
            for (int i = 0; i < fields.length; i++) {
                CsvExport csvExportField = fields[i].getAnnotation(CsvExport.class);
                if (null != csvExportField && csvExportField.export()) {

                    CsvHeader csvExportHeader = fields[i].getAnnotation(CsvHeader.class);

                    if (null != csvExportHeader) {
                        headers[index] = csvExportHeader.name();
                        index++;
                    }
                }
            }
        }

        return headers;

    }
}
